// pages/car/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [
      {name:"本田",id:"1"},
      {name:"领克",id:"2"},
      {name:"丰田",id:"3"},
    ],
    bannerList:[],
    carList: [],
    tabcur: 0,
    maincur: 0,
    verticalNavTop: 0,
    load: true,
    pageNum: 1,
    priceNameInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.brandList()
    this.setData({priceNameInfo: app.globalData.priceNameInfo})
    this.getBanner()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.brandList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('到底了')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },




  TabSelect: function(e){
      this.setData({
        tabcur: e.currentTarget.dataset.tabcur,
        maincur: e.currentTarget.dataset.tabcur
      })
      this.getCarListByBrand(e.currentTarget.dataset.id)
  },
  brandList: function(){
    var cityCode = wx.getStorageSync('local_city_code')
    var that = this
    wx.request({
      url: app.globalData.apiPath + 'treeDict/brandList',
      method: 'POST',
      data: {cityCode: cityCode},
      success (res) {
        var list = res.data.data;
        that.setData({tabList: list,maincur: 0})
        if(list.length > 0){
          that.getCarListByBrand(list[0].id)
        }
      }
    })
  },
  getCarListByBrand: function(brand){
    var that = this
    var cityCode = wx.getStorageSync('local_city_code')
    wx.request({
      url: app.globalData.apiPath + 'product/list',
      method: 'POST',
      data: {brand:brand,cityCode:cityCode},
      success (res) {
        var list = res.data.data.records;
        list.forEach((item,index) => {
          item.mainImg = JSON.parse(item.mainImg)
          item.detailImgs = JSON.parse(item.detailImgs)
          item.label = JSON.parse(item.label)
          var defaultPrice = item.price1,priceIndex = 1
          while(defaultPrice == 0){
             priceIndex++
             defaultPrice = item['price'+priceIndex]
          }
          item.defaultPrice = defaultPrice==null?'0':defaultPrice
          item.defaultPriceIndex = priceIndex
        })
        that.setData({carList:list})
      }
    })
  },
  getBanner: function(){
    var that = this
    wx.request({
      url: app.globalData.apiPath + 'banner/list',
      method: 'POST',
      data: {type:2},
      success (res) {
        var list = res.data.data;
        that.setData({bannerList:list})
      }
    })
  },
  jumpPage: function(param){
    wx.navigateTo({ url: param.currentTarget.dataset.path})
  },


})